<template>
  <div class="order-detail-page">
    <van-nav-bar title="订单详情" left-arrow @click-left="goBack" />
    
    <!-- 订单状态 -->
    <div class="order-status">
      <div class="status-info">
        <van-icon :name="getStatusIcon(orderDetail.status)" color="#157658" size="24" />
        <div class="status-text">
          <div class="status-title">{{ orderDetail.statusText }}</div>
          <div class="status-desc">{{ getStatusDesc(orderDetail.status) }}</div>
        </div>
      </div>
    </div>
    
    <!-- 收货地址 -->
    <div class="address-section">
      <div class="section-title">收货地址</div>
      <div class="address-info">
        <div class="address-header">
          <span class="name">{{ orderDetail.address?.name }}</span>
          <span class="phone">{{ orderDetail.address?.phone }}</span>
        </div>
        <div class="address-detail">{{ orderDetail.address?.address }}</div>
      </div>
    </div>
    
    <!-- 商品信息 -->
    <div class="goods-section">
      <div class="section-title">商品信息</div>
      <div class="goods-list">
        <div 
          v-for="item in orderDetail.items" 
          :key="item.id" 
          class="goods-item"
          @click="goToDetail(item.id)"
        >
          <img :src="item.img" :alt="item.title" class="goods-image" />
          <div class="goods-info">
            <div class="goods-title">{{ item.title }}</div>
            <div class="goods-specs" v-if="item.specs">
              {{ formatSpecs(item.specs) }}
            </div>
            <div class="goods-price">
              <span class="price">¥{{ item.price }}</span>
              <span class="count">x{{ item.count }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 订单信息 -->
    <div class="order-info">
      <div class="section-title">订单信息</div>
      <van-cell-group>
        <van-cell title="订单号" :value="orderDetail.orderNo" />
        <van-cell title="下单时间" :value="orderDetail.createTime" />
        <van-cell title="支付时间" :value="orderDetail.payTime || '未支付'" />
        <van-cell title="发货时间" :value="orderDetail.shipTime || '未发货'" />
        <van-cell title="完成时间" :value="orderDetail.completeTime || '未完成'" />
      </van-cell-group>
    </div>
    
    <!-- 价格信息 -->
    <div class="price-info">
      <div class="section-title">价格信息</div>
      <van-cell-group>
        <van-cell title="商品总价" :value="`¥${orderDetail.totalPrice.toFixed(2)}`" />
        <van-cell title="运费" :value="`¥${orderDetail.shipping.toFixed(2)}`" />
        <van-cell title="优惠券" :value="orderDetail.couponDiscount > 0 ? `-¥${orderDetail.couponDiscount.toFixed(2)}` : '无'" />
        <van-cell title="实付金额" :value="`¥${orderDetail.finalPrice.toFixed(2)}`" />
      </van-cell-group>
    </div>
    
    <!-- 底部操作按钮 -->
    <div class="bottom-actions">
      <van-button 
        v-if="orderDetail.status === 'pending'"
        type="danger" 
        @click="payOrder"
      >
        立即付款
      </van-button>
      <van-button 
        v-if="orderDetail.status === 'delivered'"
        type="primary" 
        @click="confirmReceive"
      >
        确认收货
      </van-button>
      <van-button 
        v-if="orderDetail.status === 'completed'"
        type="default" 
        @click="buyAgain"
      >
        再次购买
      </van-button>
      <van-button 
        v-if="orderDetail.status === 'pending'"
        type="default" 
        @click="cancelOrder"
      >
        取消订单
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast, showConfirmDialog } from 'vant'

const route = useRoute()
const router = useRouter()

// 响应式数据
const orderDetail = ref({
  id: '',
  orderNo: '',
  status: '',
  statusText: '',
  totalPrice: 0,
  shipping: 0,
  couponDiscount: 0,
  finalPrice: 0,
  createTime: '',
  payTime: '',
  shipTime: '',
  completeTime: '',
  address: {},
  items: []
})

// 获取状态图标
const getStatusIcon = (status) => {
  const iconMap = {
    pending: 'clock-o',
    paid: 'logistics',
    delivered: 'send-gift-o',
    completed: 'passed'
  }
  return iconMap[status] || 'info-o'
}

// 获取状态描述
const getStatusDesc = (status) => {
  const descMap = {
    pending: '请尽快完成支付',
    paid: '商家正在为您准备商品',
    delivered: '商品正在配送中',
    completed: '交易已完成'
  }
  return descMap[status] || ''
}

// 格式化规格
const formatSpecs = (specs) => {
  if (!specs || typeof specs !== 'object') return ''
  return Object.values(specs).join('、')
}

// 获取订单详情
const getOrderDetail = async () => {
  try {
    const orderId = route.params.id
    
    // 模拟数据
    orderDetail.value = {
      id: orderId,
      orderNo: 'ORDER20241201001',
      status: 'pending',
      statusText: '待付款',
      totalPrice: 299.00,
      shipping: 10.00,
      couponDiscount: 20.00,
      finalPrice: 289.00,
      createTime: '2024-12-01 10:30:00',
      address: {
        name: '张三',
        phone: '13800138000',
        address: '北京市朝阳区xxx街道xxx小区'
      },
      items: [
        {
          id: 1,
          title: '时尚连衣裙',
          img: '/imgs/product1.jpg',
          price: 199.00,
          count: 1,
          specs: { color: '红色', size: 'M' }
        },
        {
          id: 2,
          title: '休闲T恤',
          img: '/imgs/product2.jpg',
          price: 100.00,
          count: 1,
          specs: { color: '白色', size: 'L' }
        }
      ]
    }
  } catch (error) {
    console.error('获取订单详情失败:', error)
    showToast('获取订单详情失败')
  }
}

// 支付订单
const payOrder = async () => {
  try {
    await showConfirmDialog({
      title: '确认支付',
      message: `确认支付 ¥${orderDetail.value.finalPrice.toFixed(2)} 吗？`
    })
    
    router.push('/payment-confirm')
  } catch {
    // 用户取消
  }
}

// 确认收货
const confirmReceive = async () => {
  try {
    await showConfirmDialog({
      title: '确认收货',
      message: '确认已收到商品吗？'
    })
    
    showToast('确认收货成功')
    // 刷新订单状态
    getOrderDetail()
  } catch {
    // 用户取消
  }
}

// 再次购买
const buyAgain = () => {
  showToast('已加入购物车')
  router.push('/cart')
}

// 取消订单
const cancelOrder = async () => {
  try {
    await showConfirmDialog({
      title: '取消订单',
      message: '确认取消该订单吗？'
    })
    
    showToast('订单已取消')
    // 刷新订单状态
    getOrderDetail()
  } catch {
    // 用户取消
  }
}

// 跳转到商品详情
const goToDetail = (spuId) => {
  router.push(`/detail/${spuId}`)
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 组件挂载时获取数据
onMounted(() => {
  getOrderDetail()
})
</script>

<style lang="scss" scoped>
.order-detail-page {
  background-color: #f5f5f5;
  min-height: 100vh;
  padding-bottom: 80px;
}

.order-status {
  background: linear-gradient(135deg, #157658 0%, #2d8f7a 100%);
  padding: 30px 20px;
  color: #fff;
  
  .status-info {
    display: flex;
    align-items: center;
    
    .status-text {
      margin-left: 15px;
      
      .status-title {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 5px;
      }
      
      .status-desc {
        font-size: 14px;
        opacity: 0.8;
      }
    }
  }
}

.address-section,
.goods-section,
.order-info,
.price-info {
  background: #fff;
  margin-bottom: 10px;
  
  .section-title {
    padding: 15px 20px;
    font-size: 16px;
    font-weight: bold;
    color: #333;
    border-bottom: 1px solid #f0f0f0;
  }
}

.address-info {
  padding: 15px 20px;
  
  .address-header {
    margin-bottom: 5px;
    
    .name {
      font-size: 16px;
      font-weight: bold;
      color: #333;
      margin-right: 15px;
    }
    
    .phone {
      font-size: 14px;
      color: #666;
    }
  }
  
  .address-detail {
    font-size: 14px;
    color: #333;
    line-height: 1.4;
  }
}

.goods-list {
  .goods-item {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    
    &:last-child {
      border-bottom: none;
    }
    
    .goods-image {
      width: 80px;
      height: 80px;
      object-fit: cover;
      border-radius: 4px;
      margin-right: 15px;
    }
    
    .goods-info {
      flex: 1;
      
      .goods-title {
        font-size: 14px;
        color: #333;
        margin-bottom: 5px;
        line-height: 1.4;
      }
      
      .goods-specs {
        font-size: 12px;
        color: #999;
        margin-bottom: 5px;
      }
      
      .goods-price {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .price {
          font-size: 16px;
          font-weight: bold;
          color: #ff6b6b;
        }
        
        .count {
          font-size: 14px;
          color: #999;
        }
      }
    }
  }
}

.bottom-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 15px;
  display: flex;
  gap: 10px;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
  
  .van-button {
    flex: 1;
  }
}
</style> 